using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using OpenComposite.Base.Collections;
using OpenComposite.EII.Designer.Workflow;
using OpenComposite.EII.Repository;
using WeifenLuo.WinFormsUI.Docking;

namespace OpenComposite.EII.Designer.BUSView
{
	public partial class FormVocabularyResolve : DockContent
	{
		#region Initialization
		public FormVocabularyResolve(Repository.Workflow workflow, IServiceProvider serviceProvider)
		{
			InitializeComponent();

			_serviceProvider = serviceProvider;
			_workflow = workflow;
			fillLogic();

			init();
		}
		private void init()
		{
			_bindlstResolve = new BindingList<OpenComposite.EII.Repository.Workflow.RuleParameterResolve>(
				_workflow.ExtendedProperties.WorkflowVariables);

			this.dgvParamResolve.AutoGenerateColumns = false;
			this.dgvParamResolve.DataSource = _bindlstResolve;

			this.DialogResult = DialogResult.Cancel;

			this.DockAreas = DockAreas.Float;
		}
		#endregion

		#region Public Members
		#endregion

		#region Private Members

		#region Variables

		private IServiceProvider _serviceProvider;
		private Repository.Workflow _workflow;
		BindingListEx<TreeNode> _businessObjectTree = new BindingListEx<TreeNode>();

		private BindingList<Repository.Workflow.RuleParameterResolve> _bindlstResolve;

		#endregion

		#region Methods

		private void fillLogic()
		{
			BindingListEx<LogicalActivity> logActs = Global.Data.GetLogicalActivitiesOfWorkflow(_workflow.ID);
			NamedBusinessObjectCollection bobjects = new NamedBusinessObjectCollection(); //_workflow.GetWorkflowBusinessObjects();
			//if ( _workflow.InputBusinessObject != null ) {
			//    bobjects.AddDistinct(new NamedBusinessObject(_workflow.InputBusinessObject.Name,
			//        _workflow.InputBusinessObject));
			//}
			//if ( _workflow.OutputBusinessObject != null ) {
			//    bobjects.AddDistinct(new NamedBusinessObject(_workflow.InputBusinessObject.Name,
			//        _workflow.OutputBusinessObject));
			//}
			foreach ( LogicalActivity logAct in logActs ) {
				//if ( logAct.InputBusinessObject != null ) bobjects.AddDistinct(logAct.InputBusinessObject);
				if ( logAct.OutputBusinessObject != null ) bobjects.AddDistinct(new NamedBusinessObject(logAct.OutputVariableName, logAct.OutputBusinessObject, logAct));
			}
			foreach ( WorkflowField wfField in _workflow.Interface.Fields ) {
				bobjects.Add(new NamedBusinessObject(wfField.Name, wfField.BusinessObject, wfField));
			}
			var sortedList = from p in bobjects
							 orderby p.Name
							 select p;
			foreach ( NamedBusinessObject item in sortedList ) {
				TreeNode tn = new TreeNode(item.Name);
				tn.Tag = item;
				_businessObjectTree.Add(tn);
				if ( item.BusinessObject == null ) continue;
				if ( item.BusinessObject.BusinessObjectFields.Count == 1 &&
					item.BusinessObject.BusinessObjectFields[0].IsList ) {
					tn.ForeColor = Color.Gray;
				} else {
					foreach ( BusinessObjectField field in item.BusinessObject.BusinessObjectFields ) {
						fillLogicRecursive(field, tn);
					}
				}
			}
		}

		private void fillLogicRecursive(BusinessObjectField field, TreeNode tn)
		{
			TreeNode tnChild = tn.Nodes.Add(field.Name);
			tnChild.Tag = field;
			if ( field.IsList ) {
				tnChild.ForeColor = Color.Gray;
			} else {
				if ( field.BusinessObject != null ) {
					foreach ( BusinessObjectField subField in field.BusinessObject.BusinessObjectFields ) {
						fillLogicRecursive(subField, tnChild);
					}
				}
			}
		}
		#endregion

		#region Event Handlers

		private void FormVocabularyResolve_Load(object sender, EventArgs e)
		{
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void dgvParamResolve_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex >= 0 && e.ColumnIndex == colValueItem.Index ) {
				OpenComposite.EII.Repository.Workflow.RuleParameterResolve rpr = dgvParamResolve.Rows[e.RowIndex].DataBoundItem as OpenComposite.EII.Repository.Workflow.RuleParameterResolve;
				using ( FormSelectParameterResolve frm = new FormSelectParameterResolve(_serviceProvider, _workflow, _businessObjectTree) ) {
					foreach ( Repository.Workflow.RuleParameterResolve rpr1 in _bindlstResolve ) {
						frm.ExcludedPropertyNames.Add(rpr1.ParameterName);
					}
					Rectangle rect = dgvParamResolve.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
					frm.Size = new Size(rect.Width - 2, frm.Height);
					frm.Location = dgvParamResolve.PointToScreen(new Point(rect.Left, rect.Bottom));
					frm.SelectedPath = rpr.ParameterResolve;
					if ( frm.ShowDialog() == DialogResult.OK )
						rpr.ParameterResolve = frm.SelectedPath;
				}
			}
		}

		#endregion

		#endregion
	}
}